package cn.drenal.capcap.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.drenal.capcap.common.Response;
import cn.drenal.capcap.entity.SysPermission;
import cn.drenal.capcap.entity.dto.PermissionDTO;
import cn.drenal.capcap.entity.vo.PermissionTreeVO;
import cn.drenal.capcap.service.SysPermissionService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 权限管理控制器
 */
@RestController
@RequestMapping("/permission")
@RequiredArgsConstructor
public class SysPermissionController {

    private final SysPermissionService sysPermissionService;

    /**
     * 创建权限
     *
     * @param permissionDTO 权限信息
     * @return 创建结果
     */
    @PostMapping
    @SaCheckPermission("system:permission:add")
    public Response<SysPermission> createPermission(@RequestBody PermissionDTO permissionDTO) {
        return Response.success(sysPermissionService.createPermission(permissionDTO));
    }

    /**
     * 更新权限
     *
     * @param id 权限ID
     * @param permissionDTO 权限信息
     * @return 更新结果
     */
    @PutMapping("/{id}")
    @SaCheckPermission("system:permission:edit")
    public Response<SysPermission> updatePermission(@PathVariable Long id, @RequestBody PermissionDTO permissionDTO) {
        return Response.success(sysPermissionService.updatePermission(id, permissionDTO));
    }

    /**
     * 删除权限
     *
     * @param id 权限ID
     * @return 删除结果
     */
    @DeleteMapping("/{id}")
    @SaCheckPermission("system:permission:delete")
    public Response<Void> deletePermission(@PathVariable Long id) {
        sysPermissionService.deletePermission(id);
        return Response.success();
    }

    /**
     * 获取权限详情
     *
     * @param id 权限ID
     * @return 权限详情
     */
    @GetMapping("/{id}")
    @SaCheckPermission("system:permission:query")
    public Response<SysPermission> getPermissionById(@PathVariable Long id) {
        return Response.success(sysPermissionService.getPermissionById(id));
    }

    /**
     * 获取权限列表
     *
     * @return 权限列表
     */
    @GetMapping("/list")
    @SaCheckPermission("system:permission:list")
    public Response<List<SysPermission>> getPermissionList() {
        return Response.success(sysPermissionService.getPermissionList());
    }

    /**
     * 获取权限树
     *
     * @return 权限树
     */
    @GetMapping("/tree")
    @SaCheckPermission("system:permission:list")
    public Response<List<PermissionTreeVO>> getPermissionTree() {
        return Response.success(sysPermissionService.getPermissionTree());
    }
}